package org.atlantis.grosbeak.castor.defaults;

import java.lang.reflect.Type;

import org.atlantis.grosbeak.castor.Castor;
import org.atlantis.grosbeak.castor.FailToCastException;
import org.atlantis.grosbeak.lang.Exceptions;
import org.atlantis.grosbeak.lang.Mirror;
import org.atlantis.grosbeak.lang.Strings;

public class StringToNumber implements Castor<String, Number> {

	public Type getFrom() {
		return String.class;
	}

	public Type getTo() {
		return Number.class;
	}

	public Number obverseCast(String src, Class<?> toType)
			throws FailToCastException {
		try {
			return (Number) Mirror.me(toType).getWrapperClass().getConstructor(
					String.class).newInstance(Strings.isBlank(src) ? "0" : src);
		} catch (Exception e) {
			throw Exceptions.wrapThrow(FailToCastException.class, e);
		}
	}

	public String reverseCast(Number src, Class<?> toType)
			throws FailToCastException {
		return src.toString();
	}
}
